package game.gui;

import engine.game.Manager;
import engine.graphics.Color4f;
import engine.graphics.FontManager;
import engine.graphics.Renderer;
import engine.graphics.TextureManager;
import engine.gui.* ; 
import engine.gui.events.EventMethod;
import game.objects.nonstaticObjects.ItemModel;

public class InventoryGridField extends GridField {

	private static final long serialVersionUID = 1L;
		
	private ItemModel item ; 
	private final InventoryGrid grid ;   
	private boolean selected ; 
	
	public InventoryGridField(Manager m, InventoryGrid parent, int w, int h, int id) {
		super(m, parent, w, h, id);
		this.grid = parent ; 
	}
	
	public void select(){
		this.selected = true ; 
	}
	public void deselect(){
		this.selected = false ; 
	}
	
	public boolean isSelected(){
		return this.selected ; 
	}
	
	@SuppressWarnings("serial")
	public void init(){
		super.init(); 
		this.onClick(new EventMethod(){
			public void run(){
				select(); 
				grid.selFieldID = getID(); 
			}
		});
	}


	public ItemModel getItem() {
		return this.item ; 
	}
	
	public void removeContent(){
		this.item = null ; 
		super.removeContent() ; 
	}
	
	public void update(){
		super.update(); 
	}
	
	public void addNewItem(final ItemModel item){
		this.item = item ; 
		Panel o = new Panel(this.getManager(), this, this.getObjectHeight() - 10, this.getObjectHeight() - 10);
		o.padding(5, 5);
		o.changeNormalColor(new Color4f(1,1,1,1));
		o.enableTexturing(); 
		o.addNewAnimation(ItemModel.ITEM_ANIM_NAME, item.getAnimations().getAnimationByName(ItemModel.ITEM_ANIM_NAME));
		this.changeCaption(item.getItem().getName());
		if(!this.getLabel().getFontName().equals(FontManager.S_FONT_13))
			this.setFontName(FontManager.S_FONT_13);
		this.label.changeNormalColor(new Color4f(0.0f,0.0f,0.0f,0.65f));
		int h = FontManager.getFontByName(this.label.getFontName()).getFont().getSize();
		int lw = this.label.getCaption().length()*(h-3) ; 
		this.labelPadding(0, 30); 
		this.label.changeSize(lw, h); 
		this.label.setCaption(" "+this.label.getCaption());
		this.addContent(o);
	}
	
	public void render(){
		if(this.isVisible()){
			Renderer.renderTexturedObject(this);
			if(this.hasContent()){
				if(this.getChildByName("content").isTextured())
					TextureManager.bindTexture(ItemModel.ITEM_ANIM_NAME) ; 
				this.getChildByName("content").render(); 
			}
			if(this.MouseIn()){
				this.label.render();
				Renderer.renderTexturedObject(this.label, org.lwjgl.opengl.GL11.GL_LINE_LOOP, 
						new Color4f(0.15f, 0.15f, 0.15f, 1.0f));
			}
			if(this.isSelected()){
				org.lwjgl.opengl.GL11.glLineWidth(2.0f); 
				Renderer.renderTexturedObject(this, org.lwjgl.opengl.GL11.GL_LINE_LOOP, 
						new Color4f(1.0f, 0.4f, 0.0f, 1.0f));
				org.lwjgl.opengl.GL11.glLineWidth(1.0f); 
			}else
				Renderer.renderTexturedObject(this, org.lwjgl.opengl.GL11.GL_LINE_LOOP, 
						new Color4f(0.15f, 0.15f, 0.15f, 1.0f));
		}
	}
	
	public void getInput() {
		if(this.isVisible()){
			if(this.isClicked() && this.getThread() != null)
				this.getThread().click() ;
		}
	}
	
}
